﻿using Cheems.HMI.Models;
using Cheems.HMI.ViewModels;
using Cheems.HMI.ViewModels.Base;
using Cheems.HMI.ViewModels.Interface;
using Cheems.HMI.Views;
using Cheems.HMI.Views.UserManagement;
using Cheems.Interface.AI;
using Cheems.Interface.Configuration;
using Cheems.Interface.Media;
using Cheems.Interface.Models.UserManagement;
using Cheems.Interface.SQL;
using Cheems.Interface.UserManagement;
using Cheems.Service.AI;
using Cheems.Service.Configuration;
using Cheems.Service.Media;
using Cheems.Service.Models;
using Cheems.Service.SQL;
using Cheems.Service.UserManagement;
using DryIoc;
using LiteDB;
using Microsoft.VisualBasic.Logging;
using Prism.DryIoc;
using Prism.Ioc;
using System.IO;
using System.Windows;

namespace Cheems.HMI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        /// <summary>
        /// IOC注册服务
        /// </summary>
        /// <param name="containerRegistry"></param>
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {

            containerRegistry.RegisterSingleton<MainWindow>();
            containerRegistry.Register<ChatWindow>();
            containerRegistry.Register<LoginWindow>();
            containerRegistry.Register<UserManagementWindow>();

            // 初始化view model
            containerRegistry.RegisterSingleton<IAppVM, AppVM>();
            containerRegistry.RegisterSingleton<IUIEventVM, UIEventVM>();
            containerRegistry.RegisterSingleton<IUserManagementVM, UserManagementVM>();


            containerRegistry.RegisterSingleton<ISound, SoundService>(); // 初始化音频服务
            containerRegistry.RegisterSingleton<IChat, ChatService>(); // 初始化聊天服务

            #region 用户管理服务
            string userMangementDbPath = $"Filename={Path.Combine(Directory.GetCurrentDirectory(), "User.db")};Password=Cheems;";
            var container = (IContainer)containerRegistry.GetContainer();
            container.Register<IBaseRepository<T_User>, LitedbRepository<T_User>>(
            Reuse.Singleton,
               Made.Of(() => new LitedbRepository<T_User>(userMangementDbPath)));
            containerRegistry.RegisterSingleton<IUserManagement, UserManagementService>(); // 初始化用户管理服务
            #endregion


            container.RegisterDelegate<IConfig<HMIConfig>>(resolver =>
                new ConfigService<HMIConfig>(HMIConfig.Default), reuse: Reuse.Singleton);
            container.RegisterDelegate<IConfig<ChatConfig>>(resolver =>
                new ConfigService<ChatConfig>(ChatConfig.Default), reuse: Reuse.Singleton);
        }
    }
}
